<?php
namespace common\core\config;

use Yii;
use yii\base\InvalidParamException;
use common\core\base\BaseBackendController;

abstract class BaseConfigController extends BaseBackendController
{

    public function doConfig($model, $view = null)
    {
        if (! ($model instanceof ConfigForm))
        {
            throw new InvalidParamException('model must be instance of ConfigForm');
        }
        
        if (Yii::$app->request->isPost)
        {
            if ($model->load(Yii::$app->request->post())) {
                $model->saveAllConfigItem();
                return $this->refresh();
            }
        }
        else
        {
            if ($view === null)
            {
                $view = $this->action->id;
            }
            
            $model->initAllConfigItem();
            return $this->render($view, [
                'model' => $model
            ]);
        }
    }
}
